package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.WdclzdgmdBaDetailBo;
import com.ruoyi.report_wjjhl.domain.vo.WdclzdgmdBaDetailVo;
import com.ruoyi.report_wjjhl.domain.WdclzdgmdBaDetail;
import com.ruoyi.report_wjjhl.mapper.WdclzdgmdBaDetailMapper;
import com.ruoyi.report_wjjhl.service.IWdclzdgmdBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 最大干密度\最佳含水率试验检测记录详细记录Service业务层处理
 *
 * @author zyb
 * @date 2024-07-30
 */
@RequiredArgsConstructor
@Service
public class WdclzdgmdBaDetailServiceImpl implements IWdclzdgmdBaDetailService {

    private final WdclzdgmdBaDetailMapper baseMapper;

    /**
     * 查询最大干密度\最佳含水率试验检测记录详细记录
     */
    @Override
    public WdclzdgmdBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询最大干密度\最佳含水率试验检测记录详细记录列表
     */
    @Override
    public TableDataInfo<WdclzdgmdBaDetailVo> queryPageList(WdclzdgmdBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WdclzdgmdBaDetail> lqw = buildQueryWrapper(bo);
        Page<WdclzdgmdBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询最大干密度\最佳含水率试验检测记录详细记录列表
     */
    @Override
    public List<WdclzdgmdBaDetailVo> queryList(WdclzdgmdBaDetailBo bo) {
        LambdaQueryWrapper<WdclzdgmdBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WdclzdgmdBaDetail> buildQueryWrapper(WdclzdgmdBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WdclzdgmdBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, WdclzdgmdBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getShiycs() != null, WdclzdgmdBaDetail::getShiycs, bo.getShiycs());
        lqw.eq(bo.getTongrj() != null, WdclzdgmdBaDetail::getTongrj, bo.getTongrj());
        lqw.eq(bo.getTongzl() != null, WdclzdgmdBaDetail::getTongzl, bo.getTongzl());
        lqw.eq(bo.getTongjstzl() != null, WdclzdgmdBaDetail::getTongjstzl, bo.getTongjstzl());
        lqw.eq(bo.getShitzl() != null, WdclzdgmdBaDetail::getShitzl, bo.getShitzl());
        lqw.eq(bo.getShimd() != null, WdclzdgmdBaDetail::getShimd, bo.getShimd());
        lqw.eq(bo.getGanmd() != null, WdclzdgmdBaDetail::getGanmd, bo.getGanmd());
        lqw.eq(StringUtils.isNotBlank(bo.getHeh1()), WdclzdgmdBaDetail::getHeh1, bo.getHeh1());
        lqw.eq(bo.getHezl1() != null, WdclzdgmdBaDetail::getHezl1, bo.getHezl1());
        lqw.eq(bo.getHejstzl1() != null, WdclzdgmdBaDetail::getHejstzl1, bo.getHejstzl1());
        lqw.eq(bo.getHejgtzl1() != null, WdclzdgmdBaDetail::getHejgtzl1, bo.getHejgtzl1());
        lqw.eq(bo.getShuifzl1() != null, WdclzdgmdBaDetail::getShuifzl1, bo.getShuifzl1());
        lqw.eq(bo.getGantzl1() != null, WdclzdgmdBaDetail::getGantzl1, bo.getGantzl1());
        lqw.eq(bo.getHansl1() != null, WdclzdgmdBaDetail::getHansl1, bo.getHansl1());
        lqw.eq(StringUtils.isNotBlank(bo.getHeh2()), WdclzdgmdBaDetail::getHeh2, bo.getHeh2());
        lqw.eq(bo.getHezl2() != null, WdclzdgmdBaDetail::getHezl2, bo.getHezl2());
        lqw.eq(bo.getHejstzl2() != null, WdclzdgmdBaDetail::getHejstzl2, bo.getHejstzl2());
        lqw.eq(bo.getHejgtzl2() != null, WdclzdgmdBaDetail::getHejgtzl2, bo.getHejgtzl2());
        lqw.eq(bo.getShuifzl2() != null, WdclzdgmdBaDetail::getShuifzl2, bo.getShuifzl2());
        lqw.eq(bo.getGantzl2() != null, WdclzdgmdBaDetail::getGantzl2, bo.getGantzl2());
        lqw.eq(bo.getHansl2() != null, WdclzdgmdBaDetail::getHansl2, bo.getHansl2());
        lqw.eq(bo.getHanslpjz() != null, WdclzdgmdBaDetail::getHanslpjz, bo.getHanslpjz());
        lqw.eq(bo.getHeh1Id() != null, WdclzdgmdBaDetail::getHeh1Id, bo.getHeh1Id());
        lqw.eq(bo.getHeh2Id() != null, WdclzdgmdBaDetail::getHeh2Id, bo.getHeh2Id());
        return lqw;
    }

    /**
     * 新增最大干密度\最佳含水率试验检测记录详细记录
     */
    @Override
    public Boolean insertByBo(WdclzdgmdBaDetailBo bo) {
        WdclzdgmdBaDetail add = BeanUtil.toBean(bo, WdclzdgmdBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改最大干密度\最佳含水率试验检测记录详细记录
     */
    @Override
    public Boolean updateByBo(WdclzdgmdBaDetailBo bo) {
        WdclzdgmdBaDetail update = BeanUtil.toBean(bo, WdclzdgmdBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WdclzdgmdBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除最大干密度\最佳含水率试验检测记录详细记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
